package com.wandian.audio.mvvm.audiolibrary;

import android.os.Bundle;

import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;
import androidx.viewpager.widget.ViewPager;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.flyco.tablayout.listener.OnTabSelectListener;
import com.mylib.libcore.adapter.BasePagerAdapter;
import com.mylib.libcore.config.RouterConfig;
import com.mylib.libcore.mvvm.BaseMVVMActivity;
import com.mylib.libcore.utils.ActivityUtil;
import com.mylib.libcore.utils.UIUtils;
import com.wandian.audio.R;
import com.wandian.audio.databinding.AudioActivityLibraryBinding;
import com.wandian.audio.mvvm.audiolibrary.audiolibrarypage.AudioLibraryChildFragment;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 * @date 2019/12/18-14:28
 * @description DJ排行榜activity
 **/
@Route(path = RouterConfig.ROUTER_ACTIVITY_AUDIO_LIBRARY)
public class AudioLibraryActivity extends BaseMVVMActivity<AudioLibraryActivityViewModel> {
    private AudioActivityLibraryBinding dataBinding;
    private BasePagerAdapter myAdapter;
    private List<String> titleList=new ArrayList<>();
    private List<Fragment> fragments = new ArrayList<>();
    @Override
    public void init(Bundle savedInstanceState) {
        dataBinding = DataBindingUtil.setContentView(this, R.layout.audio_activity_library);
        dataBinding.setLifecycleOwner(this);
        dataBinding.setViewModel(mViewModel);
//        mViewModel.getTitleListData();
        initViewData();
        initClink();
    }

    private void initViewData(){
        ActivityUtil.getInstance().showStatusView(this,dataBinding.viewSysTop,true);
        dataBinding.bindTransparentToolBar.toolBarTvTitle.setText("曲库");
        titleList.clear();
        fragments.clear();
        titleList.add("人气榜");
        titleList.add("新歌榜");
        titleList.add("收藏榜");
        initTabPage();
    }

    private void initClink(){
        dataBinding.bindTransparentToolBar.toolBarIvBack.setOnClickListener(v -> {
            finish();
        });
    }

    //初始化tabpage
    private void initTabPage() {
        for(int i=0;i<titleList.size();i++){
            fragments.add(AudioLibraryChildFragment.newInstance(titleList.get(i).trim()));
        }
        myAdapter = new BasePagerAdapter(getSupportFragmentManager(), fragments,titleList);
        if(UIUtils.isEmpty(dataBinding.viewPager.getAdapter())){
            dataBinding.viewPager.setAdapter(myAdapter);
            dataBinding.viewPager.setOffscreenPageLimit(titleList.size());
            dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        }
        if(!UIUtils.isEmpty(myAdapter)){
            myAdapter.notifyDataSetChanged();
        }
        //预加载数量
        dataBinding.viewPager.setOffscreenPageLimit(1);
        dataBinding.tabLayout.setViewPager(dataBinding.viewPager);
        dataBinding.tabLayout.setOnTabSelectListener(new OnTabSelectListener() {
            @Override
            public void onTabSelect(int position) {
            }

            @Override
            public void onTabReselect(int position) {
            }
        });
        dataBinding.viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {

            }

            @Override
            public void onPageScrollStateChanged(int state) {
            }
        });
    }


}
